package yhao.infra.feature.aliyun.oss.core;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.apache.commons.lang3.StringUtils;
import yhao.infra.feature.aliyun.oss.AliyunOssProperties;

/**
 * @author yoara
 * <p> Created on 2019/6/6 </p>
 */
public class BucketInfo {
    private String url;
    private String name;
    private String endpoint;
    private OSS ossClient;
    private String prefix = "";

    public BucketInfo(String accessKey,String secretKey,AliyunOssProperties.Bucket info) {
        if (StringUtils.isBlank(info.getUrl())) {
            throw new RuntimeException("阿里云Oss组件需要bucket的url");
        }
        if (StringUtils.isBlank(info.getEndpoint())) {
            throw new RuntimeException("阿里云Oss组件需要bucket的endpoint");
        }
        if (StringUtils.isBlank(accessKey)) {
            throw new RuntimeException("阿里云Oss组件需要bucket的accessKey");
        }
        if (StringUtils.isBlank(secretKey)) {
            throw new RuntimeException("阿里云Oss组件需要bucket的secretKey");
        }
        this.url = info.getUrl();
        this.name = info.getName();
        this.endpoint = info.getEndpoint();
        this.prefix = info.getPrefix();
        // 创建OSSClient实例。
        ossClient = new OSSClientBuilder().build(endpoint, accessKey, secretKey, info.getClientConf());
    }

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getUrl() {
        return url;
    }

    public OSS getOssClient() {
        return ossClient;
    }

    public String getEndpoint() {
        return endpoint;
    }

    public String getName() {
        return name;
    }
}
